package com.boot2523.service;

import com.boot2523.bean.Emp;
import com.boot2523.mapper.EmpMapper;
import jdk.internal.util.xml.impl.Input;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.util.Collections;
import java.util.List;
@Service
public class EmpS implements EmpMapper {
    @Resource
    public EmpMapper em;
    @Override
    public int insert(Emp e) {
        return em.insert(e);
    }

    @Override
    public List<Emp> all(String path) {
        //得到集合
        List<Emp> data=em.all(path);
        for (Emp e:data) {
           //复制
            try {
                //有图片再复制
                if(e.getEpic2()!=null&&!e.getEpic().equals(""))
                copy(path,e);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            //清空二进制
            e.setEpic2(null);
        }
        return data;
    }
    public void copy(String path,Emp e) throws IOException {
        //输入流
       InputStream is=e.getEpic2();
       //输出流
       OutputStream os=new FileOutputStream(path+"/"+e.getEpic());
        byte de[]=new byte[1024];
        //读
        int len=is.read(de);
        while (len!=-1){
            os.write(de,0,len);
            //再读
            len=is.read(de);
        }
        is.close();
        os.close();
    }
    @Override
    public int del(int id) {
        return em.del(id);
    }

    @Override
    public Emp one(String path, int id) {
        Emp e=em.one(path,id);
        try {
            copy(path,e);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        e.setEpic2(null);
        return e;
    }

    @Override
    public Emp one2(int id) {
        Emp one=em.one2(id);
        one.setEpic2(null);
        return one;
    }

    @Override
    public int update(Emp e) {
        return em.update(e);
    }

    @Override
    public int updateSelective(Emp e) {
        return em.updateSelective(e);
    }
}
